<template>
  <div>
    <!-- 头部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 头部面包屑导航 -->
    <!-- 卡片显示区域 -->
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 添加角色按钮 -->
      <!-- 角色列表区域j -->
      <el-table :data="rolelist"
                ref="roleTable"
                border
                stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item1, i1) in scope.row.children"
                    :key="item1.id+i1">
              <el-col :span="5">
                <el-tag closable
                        @close="onTagCloseClicked(scope.row, item1)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2, i2) in item1.children"
                        :key="item2.id+i2">
                  <el-col :span="8">
                    <el-tag type="success"
                            closable
                            @close="onTagCloseClicked(scope.row, item2)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="16">
                    <el-tag type="warning"
                            closable
                            @close="onTagCloseClicked(scope.row, item3)"
                            v-for="(item3, i3) in item2.children"
                            :key="item3.id+i3">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 展开列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称"
                         prop="roleName"></el-table-column>
        <el-table-column label="角色描述"
                         prop="roleDesc"></el-table-column>
        <el-table-column label="操作"
                         width="300px">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-edit">编辑</el-button>
            <el-button size="mini"
                       type="danger"
                       icon="el-icon-delete">删除</el-button>
            <el-button size="mini"
                       type="warning"
                       @click="onRoleClicked(scope.row)"
                       icon="el-icon-setting">分配角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 角色列表区域j -->
    </el-card>
    <!-- 卡片显示区域 -->

    <!-- 分配角色二级框 -->
    <el-dialog title="分配权限"
               :visible.sync="roleDialogVisible"
               width="50%">
      <el-tree show-checkbox
               node-key="id"
               ref="rightsTree"
               default-expand-all
               :default-checked-keys="defKeys"
               :data="rightslist"
               :props="rightProps"></el-tree>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="roleDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="onRightsOKClicked">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配角色二级框 -->
  </div>
</template>

<script>
export default {
  created() {
    this.getRoleList()
  },
  data() {
    return {
      rolelist: [],
      roleDialogVisible: false,
      rightslist: [],
      rightProps: {
        label: 'authName',
        children: 'children'
      },
      defKeys: [''],
      curRow: ''
      // extends: [],
      // rowKey(row) {
      //   return row.id
      // }
    }
  },
  methods: {
    async getRoleList(row = null) {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      this.rolelist = res.data
      // if (row) {
      //   // setTimeout(_ => {
      //   //   this.$refs.roleTable.toggleRowExpansion(row, true)
      //   // }, 1000)
      //   this.extends.push(row.id)
      // }
      // console.log(this.rolelist)
    },

    async onTagCloseClicked(role, item) {
      const result = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(
        err => err
      )
      if (result !== 'confirm') {
        return this.$msg.info('已经取消操作')
      }
      // this.$msg.success('操作成功')
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${item.id}`)
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      role.children = res.data
      this.$msg.success(res.meta.msg)
    },

    async onRoleClicked(role) {
      this.curRow = role
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      this.rightslist = res.data
      this.defKeys = []
      this.getLeafKeys(role, this.defKeys)
      // console.log(role)
      this.roleDialogVisible = true
      // console.log(this.rightslist)
    },

    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(e => this.getLeafKeys(e, arr))
    },

    async onRightsOKClicked() {
      const keys = [
        ...this.$refs.rightsTree.getCheckedKeys(),
        ...this.$refs.rightsTree.getHalfCheckedKeys()
      ]
      const idStr = keys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.curRow.id}/rights`, { rids: idStr })
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      this.$msg.success(res.meta.msg)
      this.roleDialogVisible = false
      this.getRoleList()
    },

    getDefKeys(role) {
      if (role.children) {
        role.children.forEach(e => {
          if (e.children) {
            e.children.forEach(el => {
              if (el.children) {
                el.children.forEach(v => {
                  this.defKeys.push(v.id)
                })
              }
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.el-table {
  .el-row {
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;
  }
  .el-row:nth-of-type(1) {
    border-top: 1px solid #eee;
  }
}

.el-col {
  > .el-row:nth-of-type(1) {
    border-top: none;
  }
  > .el-row:last-of-type {
    border-bottom: none;
  }
}
</style>
